<?
class icd10 extends module{

    // Author: Herman Tolentino MD
    // CHITS Project 2004
    // ICD10 table structures derived from care2x
    //

    function icd10() {
        //
        // do not forget to update version
        //
        $this->author = 'Herman Tolentino MD';
        $this->version = "0.1-".date("Y-m-d");
    }

    // --------------- STANDARD MODULE FUNCTIONS ------------------

    function init_deps() {
    //
    // insert dependencies in module_dependencies
    //
        $sql = "insert into module_dependencies (module_id, req_module) ".
               "values ('icd10','module')";
        $result = mysql_query($sql);

        $sql = "insert into module_dependencies (module_id, req_module) ".
               "values ('icd10','healthcenter')";
        $result = mysql_query($sql);

    }

    function init_stats() {
    }

    function init_help() {
    }

    function init_menu() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        // menu entries

        $sql = "insert into module_menu (module_id, menu_title, menu_cat, menu_action) ".
               "values ('icd10', 'ICD Codes', 'LIBRARIES', '_icd10')";
        $result = mysql_query($sql);

        // put in more details
        $sql = "update modules set module_desc = 'CHITS Library - ICD10', ".
               "module_version = '".$this->version."', module_author = '".$this->author."' ".
               "where module_id = 'icd10';";
        $result = mysql_query($sql);

    }

    function init_lang() {
    //
    // insert necessary language directives
    // NOTES:
    // 1. refer to table term
    // 2. skip remarks and translationof since this term is manually entered
    //
        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_CODE','english', 'ICD CODE', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_DESCRIPTION','english', 'DESCRIPTION', 'Y');";

        $result = mysql_query($sql);
        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_CLASS_SUB','english', 'SUB CLASS', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_INCLUSIVE','english', 'INCLUSIVE', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_EXCLUSIVE','english', 'EXCLUSIVE', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_ICD_NOTES','english', 'NOTES', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('FTITLE_ICD_CODE','english', 'ICD CODE FORM', 'Y');";
        $result = mysql_query($sql);

        $sql = "insert into terms (termid, languageid, langtext, isenglish) ".
               "values ('LBL_BARANGAY_NAME','english', 'BARANGAY NAME', 'Y');";
        $result = mysql_query($sql);

    }

    function init_sql() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        // this is not auto_increment
        $sql = "CREATE TABLE `m_lib_icd10_en` (".
               "`diagnosis_code` varchar(12) NOT NULL default '',".
               "`description` text NOT NULL,".
               "`class_sub` varchar(5) NOT NULL default '',".
               "`type` varchar(10) NOT NULL default '',".
               "`inclusive` text NOT NULL,".
               "`exclusive` text NOT NULL,".
               "`notes` text NOT NULL,".
               "`std_code` char(1) NOT NULL default '',".
               "`sub_level` tinyint(4) NOT NULL default '0',".
               "`remarks` text NOT NULL,".
               "`extra_codes` text NOT NULL,".
               "`extra_subclass` text NOT NULL".
               ") TYPE=MyISAM; ";
        $result = mysql_query($sql);

        // load ICD data from sql text file
        // first text file
        if (file_exists("../sql/icd10_a.sql")) {
            if ($fp = fopen("../sql/icd10_a.sql", "r")) {
                while (!feof ($fp)) {
                    $sql_buffer = fgets($fp, 4096);
                    mysql_query($sql_buffer);
                }
                fclose ($fp);
            }
        }
        // second text file
        if (file_exists("../sql/icd10_b.sql")) {
            if ($fp = fopen("../sql/icd10_b.sql", "r")) {
                while (!feof ($fp)) {
                    $sql_buffer = fgets($fp, 4096);
                    mysql_query($sql_buffer);
                }
                fclose ($fp);
            }
        }
    }

    function drop_tables() {
        $sql = "DROP TABLE `m_lib_icd10_en`;";
        $result = mysql_query($sql);

    }

    // --------------- CUSTOM MODULE FUNCTIONS ------------------

    function _icd10() {
    //
    // main ICD10
    //
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('icd10')) {
            return print($exitinfo);
        }

        if ($post_vars["submiticd10"]) {
            //$this->process_icd10($menu_id, $post_vars, $get_vars);
            header("location: ".$_SERVER["PHP_SELF"]."?page=LIBRARIES&menu_id=$menu_id");
        }
        //$this->display_icd10($menu_id);
        $this->form_icd10($menu_id, $post_vars, $get_vars);
    }

    function form_icd10 () {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            //print_r($arg_list);
            if ($get_vars["diagnosis_code"]) {
                $sql = "select patient_id, healthcenter_id, patient_lastname, ".
                       "patient_firstname, patient_middle, patient_dob, patient_gender ".
                       "from m_patient where patient_id = '".$get_vars["patient_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $patient = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id' name='form_patient' method='post'>";
        print "<tr valign='top'><td>";
        print "<font color='#99CC00' size='5'><b>".FTITLE_ICD_CODE."</b></font>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<b>NOTE:</b> This form is for old patients.<br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_CODE."</span><br> ";
        print "<input type='text' class='textbox' ".($get_vars["patient_id"]?'disabled':'')." name='patient_firstname' value='".($patient["patient_firstname"]?$patient["patient_firstname"]:$post_vars["patient_firstname"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_DESCRIPTION."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_description' style='border: 1px solid #000000'>".($icd10["inclusive"]?$icd10["inclusive"]:$post_vars["icd10_inclusive"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_CLASS_SUB."</span><br> ";
        print "<input type='text' class='textbox' ".($get_vars["patient_id"]?'disabled':'')." name='patient_lastname' value='".($patient["patient_lastname"]?$patient["patient_lastname"]:$post_vars["patient_lastname"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_INCLUSIVE."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_inclusive' style='border: 1px solid #000000'>".($icd10["inclusive"]?$icd10["inclusive"]:$post_vars["icd10_inclusive"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_EXCLUSIVE."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_exclusive' style='border: 1px solid #000000'>".($icd10["exclusive"]?$icd10["exclusive"]:$post_vars["icd10_exclusive"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_NOTES."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_notes' style='border: 1px solid #000000'>".($icd10["notes"]?$icd10["notes"]:$post_vars["icd10_notes"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_REMARKS."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_notes' style='border: 1px solid #000000'>".($icd10["notes"]?$icd10["notes"]:$post_vars["icd10_notes"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_EXTRACODES."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_notes' style='border: 1px solid #000000'>".($icd10["notes"]?$icd10["notes"]:$post_vars["icd10_notes"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_EXTRACLASS."</span><br> ";
        print "<textarea rows='5' cols='40' class='textbox' name='icd_notes' style='border: 1px solid #000000'>".($icd10["notes"]?$icd10["notes"]:$post_vars["icd10_notes"])."</textarea><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_STANDARD_CODE."</span><br> ";
        print "<input type='checkbox' name='std_code' ".(($icd10["std_code"]?$icd10["std_code"]:$post_vars["std_code"])=="Y"?"checked":"")."> Check if standard code<br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_ICD_SUBLEVEL."</span><br> ";
        print "<select name='patient_gender' ".($get_vars["patient_id"]?'disabled':'')." class='textbox'>";
        print "<option ".($icd10["sub_level"]=='1'?'selected':'')." value='1'>Level 1</option>";
        print "<option ".($icd10["sub_level"]=='2'?'selected':'')." value='2'>Level 2</option>";
        print "<option ".($icd10["sub_level"]=='3'?'selected':'')." value='3'>Level 3</option>";
        print "<option ".($icd10["sub_level"]=='4'?'selected':'')." value='4'>Level 4</option>";
        print "<option ".($icd10["sub_level"]=='5'?'selected':'')." value='5'>Level 5</option>";
        print "</select>";
        print "</td></tr>";
        print "<tr><td>";
        print "<br><input type='submit' value = 'Add Code' class='textbox' name='submitpatient' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "</form>";
        print "</table><br>";
    }
}
?>
